package com.xie.smfs.event.screen;

import com.xie.smfs.registry.ModScreenHandlers;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import com.xie.smfs.item.GhostPorcelainItem;
import com.xie.smfs.Smfs;
import net.minecraft.network.PacketByteBuf;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.server.network.ServerPlayerEntity;

public class GhostControlScreenHandler extends ScreenHandler {

    // 修改工厂类
    public static class GhostControlFactory implements ExtendedScreenHandlerFactory, net.minecraft.screen.NamedScreenHandlerFactory {
        @Override
        public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) {
            // 写入额外数据到客户端（如无需传递数据可留空）
        }

        @Override
        public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
            Smfs.LOGGER.info("[工厂] 开始创建厉鬼控制界面处理器: syncId={}, player={}",
                    syncId, player.getName().getString());

            // 创建服务器端的处理器
            ScreenHandler handler = new GhostControlScreenHandler(syncId, inv, new SimpleInventory(GHOST_SLOT_COUNT));

            Smfs.LOGGER.info("[工厂] 厉鬼控制界面处理器创建成功: {}", handler.getClass().getSimpleName());
            return handler;
        }

        @Override
        public Text getDisplayName() {
            return Text.translatable("screen.smfs.ghost_control");
        }
    }

    private final Inventory ghostInventory;
    private static final int GHOST_SLOT_COUNT = 6;

    // 在 GhostControlScreenHandler 构造函数中修改槽位位置
    // 服务器端构造函数
    public GhostControlScreenHandler(int syncId, PlayerInventory playerInventory, Inventory ghostInventory) {
        super(ModScreenHandlers.GHOST_CONTROL_SCREEN_HANDLER, syncId);

        Smfs.LOGGER.info("[服务器] 服务器端构造函数被调用: syncId={}", syncId);

        checkSize(ghostInventory, GHOST_SLOT_COUNT);
        this.ghostInventory = ghostInventory;
        ghostInventory.onOpen(playerInventory.player);

        // 圆形分布的6个槽位
        int centerX = 89; // 界面中心X坐标
        int centerY = 61; // 界面中心Y坐标（与屏幕渲染匹配）
        int radius = 30;  // 圆半径

        // 计算6个槽位的圆形分布位置
        for (int i = 0; i < GHOST_SLOT_COUNT; i++) {
            double angle = 2 * Math.PI * i / GHOST_SLOT_COUNT;
            int x = centerX + (int)(radius * Math.cos(angle)) - 8; // -8 是为了让槽位居中
            int y = centerY + (int)(radius * Math.sin(angle)) - 8; // -8 是为了让槽位居中

            this.addSlot(new GhostSlot(ghostInventory, i, x, y));
        }


    }

    // 客户端构造函数 - 用于数据包解码
    public GhostControlScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
        this(syncId, playerInventory, new SimpleInventory(GHOST_SLOT_COUNT));
        Smfs.LOGGER.info("[客户端] 客户端构造函数被调用（数据包）: syncId={}", syncId);
    }

    // 简化版客户端构造函数 - 用于 ExtendedScreenHandlerType
    public GhostControlScreenHandler(int syncId, PlayerInventory playerInventory) {
        this(syncId, playerInventory, new SimpleInventory(GHOST_SLOT_COUNT));
        Smfs.LOGGER.info("[客户端] 简化客户端构造函数被调用: syncId={}", syncId);
    }

    @Override
    public boolean canUse(PlayerEntity player) {
        boolean canUse = this.ghostInventory.canPlayerUse(player);
        // 仅在权限验证失败时记录日志
        if (!canUse) {
            Smfs.LOGGER.warn("[验证] 玩家{}使用权限被拒绝", player.getName().getString());
        }
        return canUse;
    }

    // 自定义槽位类
    private static class GhostSlot extends Slot {
        public GhostSlot(Inventory inventory, int index, int x, int y) {
            super(inventory, index, x, y);
        }

        @Override
        public boolean canInsert(ItemStack stack) {
            return stack.getItem() instanceof GhostPorcelainItem;
        }
    }

    // 移除快速移动功能（无玩家物品栏）
    @Override
    public ItemStack quickMove(PlayerEntity player, int slotIndex) {
        return ItemStack.EMPTY;
    }

    @Override
    public void onClosed(PlayerEntity player) {
        super.onClosed(player);
        this.ghostInventory.onClose(player);
    }
}